{% extends "base/front_micro.html" %}

{% block container %}
    <form role="form" id="UpdatePasswordId">
        <div class="form-group">
            <label for="old_pass">请输入旧密码：</label>
            <input type="password" class="form-control" id="old_pass" name="OldPassword" placeholder="">
        </div>
        <div class="form-group">
            <label for="new_pass">请输入新密码：</label>
            <input type="password" class="form-control" id="new_pass" name="NewPassword" placeholder="">
        </div>
        <div class="form-group">
            <label for="new_repass">请确认新密码：</label>
            <input type="password" class="form-control" id="new_repass" name="NewRePassword" placeholder="">
        </div>
        <input type="hidden" name="username" value="{{ g.username }}">
        <input type="button" onclick=SubmitUpdatePassword() value="修改密码" class="btn btn-danger" />
    </form>
{% endblock %}

{% block script %}
<script type="text/javascript">
    function SubmitUpdatePassword() {
        var oldpasswd=document.getElementById("old_pass").value;
        var password=document.getElementById("new_pass").value;
        var repasswd=document.getElementById("new_repass").value;
        var matchResult=true;
        if(oldpasswd==""||password==""||repasswd==""){
            layer.alert("请确认是否有空缺项！");
            matchResult=false;
        }else if(password.length<6||password.length>30||repasswd.length<6||repasswd.length>30){
            layer.alert("密码长度应在6到30个字符之间！");
            matchResult=false;
        }else if(password!=repasswd){
            layer.alert("新密码和重复密码不同，请重新输入！");
            matchResult=false;
        }
        if (matchResult==true) {
            data = $('#UpdatePasswordId').serialize();
            console.debug(data);
            $.ajax({
                url: '{{ url_for("api.user", ChangeType="password") }}',
                method: 'PUT',
                datatype: 'json',
                data: data,
                success: function(res) {
                    console.log(res);
                    if (res.success==true) {
                        layer.msg("修改密码成功", {icon: 1, time: 3*1000});
                        parent.location.reload();
                    } else {
                        layer.alert("修改密码失败，请重试提交或报告错误！\n错误原因是：" + res.msg, {icon: 2, time: 15*1000});
                    }
                },
                error:function(e){
                    layer.alert("服务器错误，请刷新重试或报告错误！", {icon: 2, time: 15*1000});
                }
            });
        }
    };
</script>
{% endblock %}